/*
* Copyright 2025 NXP
* NXP Proprietary. This software is owned or controlled by NXP and may only be used strictly in
* accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
* activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
* terms, then you may not retain, install, activate or otherwise use the software.
*/

#include "events_init.h"
#include <stdio.h>
#include "lvgl.h"

#if LV_USE_GUIDER_SIMULATOR && LV_USE_FREEMASTER
#include "freemaster_client.h"
#endif

#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
#include <esp_log.h>
#include "simple_wifi_sta.h"

#define TAG "MAIN"
#define MIN_LOAD_INTERVAL 300 // 300ms最小间隔
static uint32_t last_screen_load_time = 0; // 用于LVGL事件消抖处理

typedef enum {
    LOAD_SCREEN_BULE,       // 加载
    LOAD_SCREEN_SET,
    LOAD_SCREEN_WIFI,
    LOAD_SCREEN_WEATHER,
    LOAD_SCREEN_CODE,
    LOAD_SCREEN_BACK_BULE_HOME,   // 返回
    LOAD_SCREEN_BACK_SET_HOME,
    LOAD_SCREEN_BACK_WIFI_HOME,
    LOAD_SCREEN_BACK_WEATHER_HOME,
    LOAD_SCREEN_BACK_CODE_HOME,
} screen_load_cmd_t;

static void async_load_screen(void * cmd_ptr)
{
    // 计算时间
    uint32_t now = xTaskGetTickCount() * portTICK_PERIOD_MS;
    if ((now - last_screen_load_time) < MIN_LOAD_INTERVAL) return;
    last_screen_load_time = now;

    screen_load_cmd_t cmd = (screen_load_cmd_t)(uintptr_t)cmd_ptr;
    switch (cmd) 
    {
        case LOAD_SCREEN_BULE:
        {   
            ui_load_scr_animation(&guider_ui, &guider_ui.screen_bule, guider_ui.screen_bule_del, &guider_ui.screen_del, setup_scr_screen_bule, LV_SCR_LOAD_ANIM_FADE_ON, 50, 50, true, false);
            break;
        }
        case LOAD_SCREEN_SET:
        {   
            ui_load_scr_animation(&guider_ui, &guider_ui.screen_set, guider_ui.screen_set_del, &guider_ui.screen_del, setup_scr_screen_set, LV_SCR_LOAD_ANIM_FADE_ON, 50, 50, false, false);
            break;
        }
        case LOAD_SCREEN_WIFI:
        {   
            ui_load_scr_animation(&guider_ui, &guider_ui.screen_wifi, guider_ui.screen_wifi_del, &guider_ui.screen_del, setup_scr_screen_wifi, LV_SCR_LOAD_ANIM_FADE_ON, 50, 50, true, false);
            break;
        }
        case LOAD_SCREEN_WEATHER:
        {   
            ui_load_scr_animation(&guider_ui, &guider_ui.screen_weather, guider_ui.screen_weather_del, &guider_ui.screen_del, setup_scr_screen_weather, LV_SCR_LOAD_ANIM_FADE_ON, 50, 50, true, false);
            break;
        }
        case LOAD_SCREEN_CODE:
        {   
            ui_load_scr_animation(&guider_ui, &guider_ui.screen_code, guider_ui.screen_code_del, &guider_ui.screen_del, setup_scr_screen_code, LV_SCR_LOAD_ANIM_FADE_ON, 50, 50, true, false);
            break;
        }
        case LOAD_SCREEN_BACK_BULE_HOME:
        {   
            ui_load_scr_animation(&guider_ui, &guider_ui.screen, guider_ui.screen_del, &guider_ui.screen_bule_del, setup_scr_screen, LV_SCR_LOAD_ANIM_FADE_ON, 50, 50, true, false);
            break;
        }
        case LOAD_SCREEN_BACK_SET_HOME:
        {   
            ui_load_scr_animation(&guider_ui, &guider_ui.screen, guider_ui.screen_del, &guider_ui.screen_set_del, setup_scr_screen, LV_SCR_LOAD_ANIM_FADE_ON, 50, 50, true, false);
            break;
        }
        case LOAD_SCREEN_BACK_WIFI_HOME:
        {   
            ui_load_scr_animation(&guider_ui, &guider_ui.screen, guider_ui.screen_del, &guider_ui.screen_wifi_del, setup_scr_screen, LV_SCR_LOAD_ANIM_FADE_ON, 50, 50, true, false);
            break;
        }
        case LOAD_SCREEN_BACK_WEATHER_HOME:
        {   
            ui_load_scr_animation(&guider_ui, &guider_ui.screen, guider_ui.screen_del, &guider_ui.screen_weather_del, setup_scr_screen, LV_SCR_LOAD_ANIM_FADE_ON, 50, 50, true, false);
            break;
        }
        case LOAD_SCREEN_BACK_CODE_HOME:
        {   
            ui_load_scr_animation(&guider_ui, &guider_ui.screen, guider_ui.screen_del, &guider_ui.screen_code_del, setup_scr_screen, LV_SCR_LOAD_ANIM_FADE_ON, 50, 50, true, false);
            break;
        }
        default:
            break;
    }

}

static void screen_Bule_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        lv_async_call(async_load_screen, (void *)LOAD_SCREEN_BULE);
        break;
    }
    default:
        break;
    }
}

static void screen_Set_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        lv_async_call(async_load_screen, (void *)LOAD_SCREEN_SET);
        break;
    }
    default:
        break;
    }
}

static void screen_WIFI_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        lv_async_call(async_load_screen, (void *)LOAD_SCREEN_WIFI);
        break;
    }
    default:
        break;
    }
}

static void screen_Weather_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        lv_async_call(async_load_screen, (void *)LOAD_SCREEN_WEATHER);
        break;
    }
    default:
        break;
    }
}

static void screen_AI_tack_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        // ui_load_scr_animation(&guider_ui, &guider_ui.screen_ai_tack, guider_ui.screen_ai_tack_del, &guider_ui.screen_del, setup_scr_screen_ai_tack, LV_SCR_LOAD_ANIM_FADE_ON, 50, 50, true, false);
        break;
    }
    default:
        break;
    }
}

static void screen_Move_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        // ui_load_scr_animation(&guider_ui, &guider_ui.screen_move, guider_ui.screen_move_del, &guider_ui.screen_del, setup_scr_screen_move, LV_SCR_LOAD_ANIM_FADE_ON, 50, 50, true, false);
        break;
    }
    default:
        break;
    }
}

static void screen_Code_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        lv_async_call(async_load_screen, (void *)LOAD_SCREEN_CODE);
        break;
    }
    default:
        break;
    }
}

void events_init_screen (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_Bule, screen_Bule_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_Set, screen_Set_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_WIFI, screen_WIFI_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_Weather, screen_Weather_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_AI_tack, screen_AI_tack_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_Move, screen_Move_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_Code, screen_Code_event_handler, LV_EVENT_ALL, ui);
}

static void screen_set_BACK_Button_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        lv_async_call(async_load_screen, (void *)LOAD_SCREEN_BACK_SET_HOME);
        break;
    }
    default:
        break;
    }
}

void events_init_screen_set (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_set_BACK_Button, screen_set_BACK_Button_event_handler, LV_EVENT_ALL, ui);
}

static void screen_code_img_1_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        lv_async_call(async_load_screen, (void *)LOAD_SCREEN_BACK_CODE_HOME);
        break;
    }
    default:
        break;
    }
}

void events_init_screen_code (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_code_img_1, screen_code_img_1_event_handler, LV_EVENT_ALL, ui);
}

static void screen_weather_btn_1_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        lv_async_call(async_load_screen, (void *)LOAD_SCREEN_BACK_WEATHER_HOME);
        break;
    }
    default:
        break;
    }
}

void events_init_screen_weather (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_weather_btn_1, screen_weather_btn_1_event_handler, LV_EVENT_ALL, ui);
}

static void screen_wifi_BACK_Button_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        lv_async_call(async_load_screen, (void *)LOAD_SCREEN_BACK_WIFI_HOME);
        break;
    }
    default:
        break;
    }
}

static void screen_wifi_SmartConfig_Button_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    static uint32_t last_click_time = 0;  // 记录上一次点击的时间
    uint32_t current_time1 = xTaskGetTickCount() * portTICK_PERIOD_MS;  // 获取当前时间（毫秒）

    switch (code) {
    case LV_EVENT_CLICKED:
    {
        // 检查时间间隔，防止短时间内多次点击
        if ((current_time1 - last_click_time) < 1500)
        {
            ESP_LOGI(TAG, "Button clicked too quickly, ignored");
            return;  // 如果时间间隔小于 1500 毫秒，则忽略此次点击
        }

        last_click_time = current_time1;  // 更新上一次点击的时间

        char cRxedChar = 'S';
        if (uxQueueSpacesAvailable(wifi_notification_queue) > 0)
        {
            xQueueSend(wifi_notification_queue, &cRxedChar, 0);
            ESP_LOGI(TAG, "Sent 'S' to WiFi task");
        }
        else
        {
            ESP_LOGE(TAG, "Queue is full, cannot send 'S'");
        }
        break;
    }
    default:
        break;
    }
}

static void screen_wifi_STA_reconnection_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    static uint32_t last_click_time = 0;  // 记录上一次点击的时间
    uint32_t current_time = xTaskGetTickCount() * portTICK_PERIOD_MS;  // 获取当前时间（毫秒）

    switch (code) {
    case LV_EVENT_CLICKED:
    {
        if ((current_time - last_click_time) < 1500)
        {
            ESP_LOGI(TAG, "Button clicked too quickly, ignored");
            return;  // 如果时间间隔小于 1500 毫秒，则忽略此次点击
        }

        last_click_time = current_time;  // 更新上一次点击的时间

        char cRxedChar = 'R';
        if (uxQueueSpacesAvailable(wifi_notification_queue) > 0)
        {
            xQueueSend(wifi_notification_queue, &cRxedChar, 0);
            ESP_LOGI(TAG, "Sent 'R' to WiFi task");
        }
        else
        {
            ESP_LOGE(TAG, "Queue is full, cannot send 'R'");
        }

        break;
    }
    default:
        break;
    }
}

void events_init_screen_wifi (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_wifi_BACK_Button, screen_wifi_BACK_Button_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_wifi_SmartConfig_Button, screen_wifi_SmartConfig_Button_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_wifi_STA_reconnection, screen_wifi_STA_reconnection_event_handler, LV_EVENT_ALL, ui);
}

static void screen_bule_BACK_Button_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        lv_async_call(async_load_screen, (void *)LOAD_SCREEN_BACK_BULE_HOME);
        break;
    }
    default:
        break;
    }
}

void events_init_screen_bule (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_bule_BACK_Button, screen_bule_BACK_Button_event_handler, LV_EVENT_ALL, ui);
}

static void screen_ai_tack_btn_1_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen, guider_ui.screen_del, &guider_ui.screen_ai_tack_del, setup_scr_screen, LV_SCR_LOAD_ANIM_FADE_ON, 50, 50, true, false);
        break;
    }
    default:
        break;
    }
}

void events_init_screen_ai_tack (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_ai_tack_btn_1, screen_ai_tack_btn_1_event_handler, LV_EVENT_ALL, ui);
}

static void screen_move_btn_1_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen, guider_ui.screen_del, &guider_ui.screen_move_del, setup_scr_screen, LV_SCR_LOAD_ANIM_FADE_ON, 50, 50, true, false);
        break;
    }
    default:
        break;
    }
}

void events_init_screen_move (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_move_btn_1, screen_move_btn_1_event_handler, LV_EVENT_ALL, ui);
}


void events_init(lv_ui *ui)
{

}




